package com.gurid.service.edu.controller.api;

import com.gurid.common.base.result.R;
import com.gurid.service.base.dto.CourseDto;
import com.gurid.service.edu.entity.Course;
import com.gurid.service.edu.entity.vo.ChapterVo;
import com.gurid.service.edu.entity.vo.WebCourseQueryVo;
import com.gurid.service.edu.entity.vo.WebCourseVo;
import com.gurid.service.edu.service.ChapterService;
import com.gurid.service.edu.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@CrossOrigin
@Tag(name = "ApiCourseController", description = "课程展示")
@RestController
@RequestMapping("/api/edu/course")
public class ApiCourseController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private ChapterService chapterService;

    @Operation(summary = "分页课程列表")
    @GetMapping("list")
    public R pageList(
            @Parameter(description = "查询对象", required = true)
            WebCourseQueryVo webCourseQueryVo){

        List<Course> courseList = courseService.webSelectList(webCourseQueryVo);

        return R.ok().data("courseList", courseList);
    }

    @Operation(summary = "根据id查询课程")
    @GetMapping("get/{courseId}")
    public R getById(
            @Parameter(description = "查询的课程id", required = true)
            @PathVariable String courseId) {

        //查询课程信息和讲师信息
        WebCourseVo webCourseVo = courseService.selectWebCourseVoById(courseId);

        //查询当前课程的嵌套章节和课时信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);

        return R.ok().data("course", webCourseVo).data("chapterVoList", chapterVoList);
    }

    @Operation(summary = "根据课程id查询课程信息")
    @GetMapping("inner/get-course-dto/{courseId}")
    public CourseDto getCourseDtoById(
            @Parameter(description = "课程id", required = true)
            @PathVariable String courseId) {
        CourseDto courseDto = courseService.getCourseDtoById(courseId);
        return courseDto;
    }

    @Operation(summary = "根据课程id更改销量")
    @GetMapping("inner/update-buy-count/{courseId}")
    public R updateBuyCountById(
            @Parameter(description = "课程id", required = true)
            @PathVariable String courseId){

        courseService.updateBuyCountById(courseId);
        return R.ok();
    }
}
